/**
 * 配置
 * @this RXFormValidation: form 验证是否通过，抽取出来
 *
 * @flow
 */
'use strict'

import ValidationUtil from './valid/ValidateUtil'
import RXFormatUtil from './valid/FormatUtil'
import ValidationType from './FormItemValidationType'
import ItemType from './FormItemType'

const FormValidation = {

  empty(value) {
    return ValidationUtil.checkEmpty(value)
  },

  /**
   * 根据`验证类型`，判断value，是否符合规则
   */
  passWithName(name, value) {
    if (!value) value = ''
    value = RXFormatUtil.formatSpace(value)
    switch (name) {
      case ValidationType.TypeEmpty:
        return !ValidationUtil.checkEmpty(value)
      case ValidationType.TypeVIN:
        return ValidationUtil.checkVIN(value)
      case ValidationType.TypeEmail:
        return ValidationUtil.checkEmail(value)
      case ValidationType.TypeIdCardNo:
        return ValidationUtil.checkIdCardNo(value)
      case ValidationType.TypePhone:
        return ValidationUtil.checkPhone(value)
      case ValidationType.TypePlateNo:
        return ValidationUtil.checkPlateNo(value)
      case ValidationType.TypeCaptcha:
        return ValidationUtil.checkCaptcha(value)
      case ValidationType.TypeGraphCaptcha:
        return ValidationUtil.checkGraphCaptcha(value)
      case ValidationType.TypeBankCard:
        return ValidationUtil.checkBankCardNo(value)
      case ValidationType.TypeCheckBoxSingle:
        return this._checkBoxValidate(value)
      case ValidationType.TypeCheckBoxMulti:
        return this._checkBoxValidate(value)
      default:
        console.warn('validators格式错误')
        return false
    }
  },

  /**
   * 验证错误 toast，`title`
   * `前缀` + `title`
   */
  messageWithName(name, title) {
    switch (name) {
      case ValidationType.TypeEmpty:
      case ValidationType.TypeCheckBoxSingle:
        return title
      case ValidationType.TypePhone:
      case ValidationType.TypePlateNo:
      case ValidationType.TypeBankCard:
      case ValidationType.TypeCaptcha:
      case ValidationType.TypeGraphCaptcha:
        return '正确的' + title
      case ValidationType.TypeCheckBoxMulti:
        return title + '至少一项'
      default:
        console.warn('validators格式错误')
        return title
    }
  },

  /**
   * 验证错误 toast，`前缀`
   * `前缀` + `title`
   */
  hintWithType(formItemType) {
    switch (formItemType) {
      case ItemType.TypeInput:
        return '请输入'
      case ItemType.TypeSelect:
        return '请选择'
      case ItemType.TypeCheckBox:
        return '请选择'
      default:
        return '请填写'
    }
  },
}

export default FormValidation